import React, { Component } from 'react'
import store from '../../redux/store'
import {createIncrementAction,createDecrementAction} from '../../redux/count_action'

export default class Count extends Component {
	state = {username:'老刘'}

	render() {
		return (
			<div>
				<h2>当前求和为：{store.getState()}</h2>
				<h4>我最爱的男人是:{this.state.username}</h4>
				<select ref={ c => this.selectNumber = c}>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
				</select>&nbsp;
				<button onClick={this.increment}>+</button>&nbsp;
				<button onClick={this.decrement}>-</button>&nbsp;
				<button onClick={this.incrementIfOdd}>当前和为奇数再+</button>&nbsp;
				<button onClick={this.incrementWait}>等一等再加</button>
			</div>
		)
	}

	//加法
	increment = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//分发一个加的action
		store.dispatch(createIncrementAction(value*1))
	}

	//减法
	decrement = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//分发一个减的action
		store.dispatch(createDecrementAction(value*1))
	}

	//奇数再加
	incrementIfOdd = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		if(store.getState() % 2){
			store.dispatch(createIncrementAction(value*1))
		}
	}

	//等一等再加
	incrementWait = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//更新状态
		setTimeout(()=>{
			store.dispatch(createIncrementAction(value*1))
		},500)
	}
}
